package homework;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import javax.sql.rowset.spi.XmlWriter;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class Test02 {
    //    2.创建Student类，将下面的人员放入List<Stdent>集合中，并将list中的数据用XML文档表示出来
//classId	stuCount	stuId	    name	  birth
//08001	     32       	0800101  	赵明   	1985-12-23
//08002	     28         0800201  	胡广飞	1985-10-05
//
//写入xml之后，再读取xml文件，并使用反射的方式使用xml中的内容值创建出两个student对象并打印。
    public static void main(String[] args) throws IOException {
        // 1.创建集合
        List<Student1> list = new ArrayList<>();
        list.add(new Student1("08001", 32, "0800101", "赵明", "1985-12-23"));
        list.add(new Student1("08002", 28, "0800201", "胡广飞", "1985-10-05"));

        // 2.创建XML文件，并将list中的数据写入XML文件
        // 创建一个文档对象
        Document document = DocumentHelper.createDocument();
        //添加根元素标签
        Element student = document.addElement("Student");
        //2.添加子标签
        Element stu1 = student.addElement("stu1");
        //添加第一个子标签的孙子标签
        Element classId = stu1.addElement("classId");
        classId.addText(list.get(0).getClassId());//添加孙子标签的内容
        Element stuCount = stu1.addElement("stuCount");
        stuCount.addText(String.valueOf(list.get(0).getStuCount()));//添加孙子标签的内容
        Element stuId = stu1.addElement("stuId");
        stuId.addText(list.get(0).getStuId());//添加孙子标签的内容
        Element name = stu1.addElement("name");
        name.addText(list.get(0).getName());//添加孙子标签的内容
        Element birth = stu1.addElement("birth");
        birth.addText(list.get(0).getDate());//添加孙子标签的内容

        //添加第二个子标签
        Element stu2 = student.addElement("stu2");
        //添加第二个子标签的孙子标签
        Element classId1 = stu2.addElement("classId");
        classId1.addText(list.get(1).getClassId());//添加第二个孙子标签的内容
        Element stuCount1 = stu2.addElement("stuCount");
        stuCount1.addText(String.valueOf(list.get(1).getStuCount()));//添加第二个孙子标签的内容
        Element stuId1 = stu2.addElement("stuId");
        stuId1.addText(list.get(1).getStuId());//添加第二个孙子标签的内容
        Element name1 = stu2.addElement("name");
        name1.addText(list.get(1).getName());//添加第二个孙子标签的内容
        Element birth1 = stu2.addElement("birth");
        birth1.addText(list.get(1).getDate());//添加第二个孙子标签的内容
        //1.创建解析对象
        OutputFormat prettyPrint = OutputFormat.createPrettyPrint();
        //2.创建流对象按格式写入
        prettyPrint.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileWriter("day15-code/src/homework/Student1.xml"), prettyPrint);

        xmlWriter.write(document);
        xmlWriter.close();
    }


}


class Student1 implements Serializable {
    @Serial
    private static final long serialVersionUID = 2902439486612805168L;
    private String classId;
    private int stuCount;
    private String stuId;
    private String name;
    private String date;

    public Student1() {
    }

    public Student1(String classId, int stuCount, String stuId, String name, String date) {
        this.classId = classId;
        this.stuCount = stuCount;
        this.stuId = stuId;
        this.name = name;
        this.date = date;
    }

    public String getClassId() {
        return classId;
    }

    public void setClassId(String classId) {
        this.classId = classId;
    }

    public int getStuCount() {
        return stuCount;
    }

    public void setStuCount(int stuCount) {
        this.stuCount = stuCount;
    }

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return stuCount == student1.stuCount && Objects.equals(classId, student1.classId) && Objects.equals(stuId, student1.stuId) && Objects.equals(name, student1.name) && Objects.equals(date, student1.date);
    }

    @Override
    public int hashCode() {
        return Objects.hash(classId, stuCount, stuId, name, date);
    }

    @Override
    public String toString() {
        return "Student1{" +
                "classId='" + classId + '\'' +
                ", stuCount=" + stuCount +
                ", stuId='" + stuId + '\'' +
                ", name='" + name + '\'' +
                ", date='" + date + '\'' +
                '}';
    }
}